<script lang="ts" setup>
import { ref, onMounted } from "vue"
import { updateCompanyApi, getCompanyApi } from "@/api/company"
import { type UpdateCompanyRequestData } from "@/api/company/types"
import { type FormInstance, type FormRules, ElMessage } from "element-plus"
import UploadImage from "@/components/UploadImage/index.vue"
import UploadImageList from "@/components/UploadImageList/index.vue"
import FormAddItem from "@/components/FormAddItem/index.vue"
defineOptions({
  // 命名当前组件
  name: "Company"
})
const loading = ref<boolean>(false)

//#region 增
const DEFAULT_FORM_DATA: UpdateCompanyRequestData = {
  name: "",
  name_en: "",
  weixin: "",
  weixin_url: "",
  phone: "",
  qq: "",
  sina_weibo: "",
  logo_url: "",
  logo2_url: "",
  logo3_url: "",
  introduction: "",
  introduction_en: "",
  address: "",
  honor_imgs: [],
  honor_list: [],
  address_en: "",
  rank_desc: "",
  partners_desc: "",
  patent_qualifications_desc: "",
  core_technology_desc: ""
}
const dialogVisible = ref<boolean>(false)
const formRef = ref<FormInstance | null>(null)
const formData = ref<UpdateCompanyRequestData>(JSON.parse(JSON.stringify(DEFAULT_FORM_DATA)))

const formRules: FormRules<UpdateCompanyRequestData> = {
  name: [{ required: true, trigger: "blur", message: "请输入公司名称" }],
  weixin: [{ required: true, trigger: "blur", message: "请填写微信号" }],
  weixin_url: [{ required: true, trigger: "blur", message: "请上传微信图片" }],
  phone: [{ required: true, trigger: "blur", message: "请填写手机号" }],
  qq: [{ required: true, trigger: "blur", message: "请填写qq号" }],
  sina_weibo: [{ required: true, trigger: "blur", message: "请填写微博号" }],
  logo_url: [{ required: true, trigger: "blur", message: "请上传logo图片" }],
  honor_imgs: [{ required: true, trigger: "blur", message: "请上传荣誉证书" }],
  // honor_list: [{ required: true, trigger: "blur", message: "请填写荣誉项" }],
  rank_desc: [{ required: true, trigger: "blur", message: "请填写福建省企业排名" }],
  partners_desc: [{ required: true, trigger: "blur", message: "请填写合作伙伴数目" }],
  patent_qualifications_desc: [{ required: true, trigger: "blur", message: "请填写专利资质数目" }],
  core_technology_desc: [{ required: true, trigger: "blur", message: "请填写核心技术数目" }],
  introduction: [{ required: true, trigger: "blur", message: "请填写简介" }],
  address: [{ required: true, trigger: "blur", message: "请填写地址" }]
}
const handleCreateOrUpdate = () => {
  formRef.value?.validate((valid: boolean, fields) => {
    if (!valid) return console.error("表单校验不通过", fields)
    loading.value = true
    const honor_imgs = JSON.stringify(formData.value.honor_imgs)
    const honor_list = JSON.stringify(formData.value.honor_list)
    updateCompanyApi({ ...formData.value, honor_imgs, honor_list })
      .then(() => {
        ElMessage.success("操作成功")
      })
      .finally(() => {
        loading.value = false
      })
  })
}

//#region 查
const getNewsDetail = () => {
  loading.value = true
  getCompanyApi()
    .then(({ data }) => {
      formData.value = data || DEFAULT_FORM_DATA
      formData.value.honor_imgs = JSON.parse(formData.value.honor_imgs)
      formData.value.honor_list = JSON.parse(formData.value.honor_list)
    })
    .catch(() => {
      formData.value = DEFAULT_FORM_DATA
    })
    .finally(() => {
      loading.value = false
    })
}
onMounted(() => {
  getNewsDetail()
})
</script>

<template>
  <div class="app-container">
    <!-- 新增/修改 -->
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>公司信息</h3>
        </div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="200px" label-position="left">
        <el-form-item prop="name" label="名称">
          <el-input v-model="formData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="name_en" label="英文名称">
          <el-input v-model="formData.name_en" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="logo_url" label="公司logo">
          <upload-image v-model="formData.logo_url" />
        </el-form-item>
        <el-form-item prop="logo2_url" label="公司logo2">
          <upload-image v-model="formData.logo2_url" />
        </el-form-item>
        <el-form-item prop="log3_url" label="公司logo3">
          <upload-image v-model="formData.logo3_url" />
        </el-form-item>
        <el-form-item prop="weixin" label="微信号">
          <el-input v-model="formData.weixin" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="weixin_url" label="微信二维码">
          <upload-image v-model="formData.weixin_url" />
        </el-form-item>
        <el-form-item prop="phone" label="手机号">
          <el-input v-model="formData.phone" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="qq" label="QQ号">
          <el-input v-model="formData.qq" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="sina_weibo" label="新浪微博号">
          <el-input v-model="formData.sina_weibo" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="honor_imgs" label="荣誉证书">
          <upload-image-list v-model="formData.honor_imgs" />
        </el-form-item>
        <form-add-item
          v-model="formData.honor_list"
          paramName="honor_list"
          param-key="荣誉项"
          param-value="荣誉项值"
          addText="添加荣誉项"
        />
        <el-form-item prop="rank_desc" label="福建省排名">
          <el-input-number v-model="formData.rank_desc" :min="1" :precision="0" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="partners_desc" label="合作伙伴数目">
          <el-input-number v-model="formData.partners_desc" :min="1" :precision="0" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="patent_qualifications_desc" label="专业资质数目">
          <el-input-number v-model="formData.patent_qualifications_desc" :min="1" :precision="0" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="core_technology_desc" label="核心技术数目">
          <el-input-number v-model="formData.core_technology_desc" :min="1" :precision="0" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="introduction" label="简介">
          <el-input v-model="formData.introduction" placeholder="请输入" type="textarea" autosize />
        </el-form-item>
        <el-form-item prop="introduction_en" label="英文简介">
          <el-input v-model="formData.introduction_en" placeholder="请输入" type="textarea" autosize />
        </el-form-item>
        <el-form-item prop="address" label="地址">
          <el-input v-model="formData.address" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="address_en" label="地址英文">
          <el-input v-model="formData.address_en" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateOrUpdate" :loading="loading">确认</el-button>
      </template>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
